#include "FileMgr.h"
#include"TTime.h"
#include"StringUtils.h"
#include<sstream>
namespace{
    static tmms::base::FileLogPtr file_log_nullptr = nullptr;
}
void tmms::base::FileMgr::OnCheck()
{
    bool day_chage{false};
    bool minute_chage{false};
    bool hour_chage{false};
    int year = 0,month = 0,day = -1,hour = -1,minute = 0,second = 0;
    TTime::Now(year,month,day,hour,minute,second);
    if(last_day_==-1){
        last_day_ = day;
        last_hour_ = hour;
        last_year_ = year;
        last_month_ = month;
        last_minute_ = minute;
    }

    if(last_day_!=day){
        day_chage = true;
    }
    if(last_hour_!=hour){
        hour_chage = true;
    }

    if(last_minute_!=minute){
        minute_chage = true;
    }
    if(!last_day_&&!last_hour_&&!last_minute_){
        return;
    }

    std::lock_guard<std::mutex> lock(mtx_);
    for(auto& l:logs_){
        if(hour_chage&&l.second->GetRotateType() == RotateType::kRotateDay){
            RotateDays(l.second);
        }
        if(day_chage&&l.second->GetRotateType() == RotateType::kRotateHour){
            RotateHour(l.second);
        }
        if(minute_chage&&l.second->GetRotateType() == RotateType::kRotateMinute){
            Rotateminute(l.second);
        }
    }
}

tmms::base::FileLogPtr tmms::base::FileMgr::GetFIleLog(const std::string &file_name)
{
    std::lock_guard<std::mutex> lock(mtx_);
    auto iter = logs_.find(file_name);
    if(iter!=logs_.end()){
        return iter->second;
    }
    FileLogPtr log = std::make_shared<FileLog>(); 
    if(!log->Open(file_name)){
        return file_log_nullptr;
    }
    logs_.emplace(file_name,log);
    return log;
}

void tmms::base::FileMgr::RemoveFileLog(const FileLogPtr &log)
{
    std::lock_guard<std::mutex> lock(mtx_);
    logs_.erase(log->FilePath());
}

void tmms::base::FileMgr::RotateDays(const FileLogPtr &file)
{
    if(file->FileSize()>0){
        char buf[128] = {0};
        sprintf(buf,"_%04d-%02d-%02d",last_year_,last_month_,last_day_);
        std::string file_path = file->FilePath();
        std::string path = StringUtils::FilePath(file->FilePath());
        std::string file_name = StringUtils::FileName(file->FilePath());
        std::string file_ext = StringUtils::Extension(file->FilePath());

        std::stringstream ss;
        ss<<path
            <<file_name
            <<buf
            <<file_ext;
        file->Rotate(ss.str());
    }
}

void tmms::base::FileMgr::RotateHour(const FileLogPtr &file)
{
    if(file->FileSize()>0){
        char buf[128] = {0};
        sprintf(buf,"_%04d-%02d-%02dT%02d",last_year_,last_month_,last_day_,last_hour_);
        std::string file_path = file->FilePath();
        std::string path = StringUtils::FilePath(file->FilePath());
        std::string file_name = StringUtils::FileName(file->FilePath());
        std::string file_ext = StringUtils::Extension(file->FilePath());

        std::stringstream ss;
        ss<<path
            <<file_name
            <<buf
            <<file_ext;
        file->Rotate(ss.str());
    }
}

void tmms::base::FileMgr::Rotateminute(const FileLogPtr &file)
{
    if(file->FileSize()>0){
        char buf[128] = {0};
        sprintf(buf,"_%04d-%02d-%02dT%02d%02d",last_year_,last_month_,last_day_,last_hour_,last_minute_);
        std::string file_path = file->FilePath();
        std::string path = StringUtils::FilePath(file->FilePath());
        std::string file_name = StringUtils::FileName(file->FilePath());
        std::string file_ext = StringUtils::Extension(file->FilePath());

        std::stringstream ss;
        ss<<path
            <<file_name
            <<buf
            <<file_ext;
        file->Rotate(ss.str());
    }
}
